package cn.hznc.controller.service.entryexitrecord;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.configuration.SystemInit;
import cn.hznc.constant.CommonConstant;
import cn.hznc.controller.service.inventory.InventoryLocationService;
import cn.hznc.domain.entity.ICabinetRecordEntity;
import cn.hznc.domain.request.inventory.AbutmentEntryExitRecordReq;
import cn.hznc.domain.request.inventory.InBoundOrOutBoundTimeReq;
import cn.hznc.exception.ToolLifeCycleException;
import cn.hznc.mapper.ConfigurationMapper;
import cn.hznc.mapper.ICabinetRecordMapper;
import cn.hznc.repository.InventoryLocationRespository;
import cn.hznc.controller.task.InventoryTask;
import cn.hznc.utils.CommonUtil;
import cn.hznc.vo.LocationNumberVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author：Administrator
 * @Date：2023/11/6 11:28
 */
@Service
@Slf4j
public class AbutmentEntryExitRecordService{

    @Resource
    private ICabinetRecordMapper iCabinetRecordMapper;

    @Resource
    private InventoryLocationService inventoryLocationService;

    @Resource
    private InventoryLocationRespository inventoryLocationRespository;

    @Resource
    private ConfigurationMapper configurationMapper;

    @Resource
    private InventoryTask inventoryTask;


    @Transactional
    public Object abutmentEntryExitRecord(AbutmentEntryExitRecordReq abutmentEntryExitRecordReq) {
        QueryWrapper<ICabinetRecordEntity> wrapper = new QueryWrapper<>();
        //QueryWrapper<ConfigurationEntity> configureWrapper = new QueryWrapper<>();
        wrapper.ge("time" , abutmentEntryExitRecordReq.getStartTime());
        SimpleDateFormat sdf = new SimpleDateFormat();

        if(abutmentEntryExitRecordReq.getFlag()){
//            configurationKey = SystemInit.configurationMap.get(CommonConstant.ICABINET_PULL_Feed_TIME)
//            configureWrapper.eq("configure_key" ,CommonConstant.ICABINET_PULL_Feed_TIME);
//            ConfigurationEntity configurationEntity = configurationMapper.selectOne(configureWrapper);
            wrapper.le("time" , SystemInit.configurationMap.get(CommonConstant.ICABINET_PULL_Feed_TIME).toString());
            wrapper.eq("type" , "加料单");
            inventoryLocationRespository.updateDate(CommonConstant.ICABINET_PULL_Feed_TIME , sdf.format(new Date()));
            SystemInit.configurationMap.put(CommonConstant.ICABINET_PULL_Feed_TIME , sdf.format(new Date()));
        }else {
//            configureWrapper.eq("configure_key" ,CommonConstant.ICABINET_PULL_Reclaimer_TIME);
//            ConfigurationEntity configurationEntity = configurationMapper.selectOne(configureWrapper);
            wrapper.le("time" , SystemInit.configurationMap.get(CommonConstant.ICABINET_PULL_Reclaimer_TIME).toString());
            wrapper.eq("type" , "取料单");
            inventoryLocationRespository.updateDate(CommonConstant.ICABINET_PULL_Reclaimer_TIME , sdf.format(new Date()));
            SystemInit.configurationMap.put(CommonConstant.ICABINET_PULL_Reclaimer_TIME , sdf.format(new Date()));
        }
        wrapper.le("time" , CommonUtil.transferDate(new Date()));
        List<ICabinetRecordEntity> iCabinetRecordEntities = iCabinetRecordMapper.selectList(wrapper);
        if(CollectionUtil.isNotEmpty(iCabinetRecordEntities)){
                for (ICabinetRecordEntity iCabinetRecordEntity : iCabinetRecordEntities) {
                    String partno = iCabinetRecordEntity.getPartno();
                    //设置入库库位
                    List<LocationNumberVo> ids = inventoryLocationRespository.qryFeedLocationNum("A002", partno);
                    if (CollectionUtil.isNotEmpty(ids)) {
                        List<LocationNumberVo> result = inventoryTask.findLocationNumber(ids , Integer.valueOf(iCabinetRecordEntity.getNumber()));
                        if(CollectionUtil.isNotEmpty(result)){
                            result.forEach(locationNumberVo -> {
                                InBoundOrOutBoundTimeReq inBoundOrOutBoundTimeReq = new InBoundOrOutBoundTimeReq();
                                inBoundOrOutBoundTimeReq.setNumber(locationNumberVo.getNumber());
                                inBoundOrOutBoundTimeReq.setBz("");
                                inBoundOrOutBoundTimeReq.setInventoryLocationId(locationNumberVo.getId());
                                inBoundOrOutBoundTimeReq.setOperator(iCabinetRecordEntity.getOperator());
                                inBoundOrOutBoundTimeReq.setPartno(partno);
                                inBoundOrOutBoundTimeReq.setPartType(iCabinetRecordEntity.getPartno());
                                inBoundOrOutBoundTimeReq.setCreateTime(iCabinetRecordEntity.getTime());
                                try {
                                    inventoryLocationService.inboundLocationTool(inBoundOrOutBoundTimeReq);
                                } catch (ToolLifeCycleException e) {
                                    log.error("sync icabinet stock error =>{}" + e.getMsg() + new Date());
                                }
                            });
                        }

                    }
                }
        }
        return Boolean.TRUE;
    }
}
